﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASBCC.Database.Service;
using System.Data;
using ASBCC.Database.Entity;

namespace BEGDC.CC.Web.EZVisit.SystemTools
{
    public partial class MailTemplateEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["TemplateID"]))
                {
                    MailTemplateTableEntity ent = MailTemplateManager.SelectByKeys(long.Parse(Request.QueryString["TemplateID"]));
                    if (ent != null)
                    {
                        //txtTemplateID.Text = ent.TemplateID + "";
                        txtTemplateCode.Text = ent.TemplateCode;
                        txtMailSubject.Text = ent.MailSubject;
                        txtMailContent.Text = ent.MailContent;
                        rblEnabled.SelectedIndex = ent.Enabled.Value ? 0 : 1;
                        txtTemplateType.Text = ent.TemplateType;
                        txtDescription.Text = ent.Description;
                    }
                }
                else
                {
                    rblEnabled.SelectedIndex = 0;
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            List<string> errors = new List<string>();
            if(string.IsNullOrEmpty(txtTemplateCode.Text.Trim()))
            {
                errors.Add("模板编码不能为空！");
            }
            if (string.IsNullOrEmpty(txtMailSubject.Text.Trim()))
            {
                errors.Add("模板标题不能为空！");
            }
            if (string.IsNullOrEmpty(txtMailContent.Text.Trim()))
            {
                errors.Add("模板内容不能为空！");
            }

            if (errors.Count > 0)
            {
                Page.RegisterClientScriptBlock("", string.Format("<script>alert('{0}');</script>", 
                    string.Join("\\n", errors.ToArray())));
                return;
            }

            if (string.IsNullOrEmpty(Request.QueryString["TemplateID"]))
            {
                MailTemplateTableEntity ent = new MailTemplateTableEntity();
                ent.TemplateID = MailTemplateManager.SelectAll().Count + 1;
                //ent.TemplateID = long.Parse(txtTemplateID.Text);
                ent.TemplateCode = txtTemplateCode.Text;
                ent.MailSubject = txtMailSubject.Text;
                ent.MailContent = txtMailContent.Text;
                ent.Enabled = bool.Parse(rblEnabled.SelectedValue);
                ent.TemplateType = txtTemplateType.Text;
                ent.Description = txtDescription.Text;

                MailTemplateManager.Insert(ent);
            }
            else
            {
                MailTemplateTableEntity ent = MailTemplateManager.SelectByKeys(long.Parse(Request.QueryString["TemplateID"]));
                if (ent != null)
                {
                    ent.TemplateCode = txtTemplateCode.Text;
                    ent.MailSubject = txtMailSubject.Text;
                    ent.MailContent = txtMailContent.Text;
                    ent.Enabled = bool.Parse(rblEnabled.SelectedValue);
                    ent.TemplateType = txtTemplateType.Text;
                    ent.Description = txtDescription.Text;
                }
                MailTemplateManager.Update(ent);
            }


            //Page.RegisterClientScriptBlock("", "<script>alert('SaveSuccessfully');window.close();</script>");
            this.ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", string.Format("alert('{0}');window.close();window.returnValue='reload';", "SaveSuccessfully"), true);
        }
    }
}
